package cn.yangliu.security.service.impl;

import cn.yangliu.security.entity.User;
import cn.yangliu.security.mapper.UserMapper;
import cn.yangliu.security.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.social.security.SocialUserDetails;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @author 杨柳
 * @contact
 * @date 2019-03-20 00:08
 */
@Service
@Slf4j
@Transactional(rollbackFor = Throwable.class)
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<User> wrapper = new QueryWrapper<>(new User().setUsername(username));
        User user = baseMapper.selectOne(wrapper);
        if (Objects.isNull(user)){
            throw new UsernameNotFoundException("用户名不存在");
        }
        return user;
    }

    @Override
    public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException {
        User user = baseMapper.selectById(userId);
        if (Objects.isNull(user)){
            throw new UsernameNotFoundException("用户不存在");
        }
        return user;
    }
}
